package com.xcdh.commons.intercept;

import cn.hutool.core.util.StrUtil;
import com.xcdh.commons.consts.StringConst;
import com.xcdh.commons.utils.UserContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserIntercept implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String userInfo = request.getHeader(StringConst.USER_KEY);
        if(StrUtil.isNotBlank(userInfo)){
            UserContext.set(userInfo);
        }
        return true ;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserContext.remove();
    }
}
